package vn.com.aptech.msh.controller;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import vn.com.aptech.msh.business.IUserBean;
import vn.com.aptech.msh.entities.User;
import vn.com.aptech.msh.utils.NavigationHelper;
import vn.com.aptech.msh.utils.ResourceBundleHelper;

/**
 *
 * @author trietto
 */
@Named("updateProfileController")
@RequestScoped
public class UpdateProfileController implements Serializable {

    @Inject
    LoginController loginController;
    @Inject
    IUserBean userBusiness;
    @Inject
    @Named("currentUser")
    User currentUser;
    User model;

    @PostConstruct
    public void init() {
        model = currentUser;
    }

    public String updateProfile() {
        try {
            User newUser = userBusiness.updateProfile(model);
            loginController.setCurrentUser(newUser);
            return NavigationHelper.toSuccessPage("updateprofile");
        } catch (Exception ex) {
            Logger.getLogger(UpdateProfileController.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get("updateProfile.failed"), "");
            FacesContext.getCurrentInstance().addMessage("frmUpdateProfile", msg);
            return null;
        }
    }

    public User getModel() {
        return model;
    }

    public void setModel(User model) {
        this.model = model;
    }
}
